#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
要表现得像list那样按照下标取出元素，需要实现__getitem__()方法
实例加[]即是调用类的__getitem__()函数，并将[]内的作为参数传递到__getitem__()函数内
"""


class Fib(object):

    def __getitem__(self, n):
        if isinstance(n, int):
            x, a, b = 0, 0, 1
            for i in range(n):
                x = a
                a, b = b, a + b
            return x
        if isinstance(n, slice):
            sta = n.start
            end = n.stop
            sep = n.step
            lis = []
            x, a, b = 0, 0, 1
            for i in range(end):
                x = a
                a, b = b, a + b
                if i >= sta:
                    lis.append(x)
            return lis[::sep]


def main():
    f = Fib()
    print(f[10])
    print(f[1:10:2])


if __name__ == "__main__":
    main()
